package com.fzshang.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.commen.bean.TableNumber;
import com.fzshang.commen.bean.layuiBean.ResultPage;
import com.fzshang.commen.bean.layuiBean.VoPage;
import com.fzshang.commen.controller.BaseController;
import com.fzshang.commen.entity.ApiResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 桌号 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-02-24
 */
@Controller
@RequestMapping("admin/tableNumber")
public class TableNumberController extends BaseController {

    /**
     * @return 主页面
     */
    @RequestMapping(value = "tableNumber.html" )
    public String tableNumber(Model model){
        model.addAttribute("userId",getUser().getId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        model.addAttribute("user",userService.list(queryWrapper));
        return "admin/tableNumber/tableNumber";
    }

    /**
     * @return 添加页面
     */
    @RequestMapping(value = "tableNumber_add.html" )
    public String tableNumber_add(){
        return "admin/tableNumber/tableNumber_add";
    }
    /**
     * @return 添加页面
     */
    @RequestMapping(value = "add.html" )
    public String add(){
        return "admin/tableNumber/add";
    }
    /**
     * @return 弹框
     */
    @RequestMapping(value = "alert.html" )
    public String alert(Long id,Model model){
        model.addAttribute("id",id);
        return "admin/tableNumber/alert";
    }
    /**
     * @return 删除桌号
     */
    @RequestMapping(value = "delete.html" )
    public String delete(Model model){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",getUser().getId());
        model.addAttribute("tableNumber",tableNumberService.list(queryWrapper));
        return "admin/tableNumber/delete";
    }

    /**
     * @param model
     * @param id
     * @return 编辑页面
     */
    @RequestMapping(value = "tableNumber_edit.html" )
    public String tableNumber_edit(Model model,Long id){
        model.addAttribute("tableNumber",tableNumberService.getById(id));
        return "admin/tableNumber/tableNumber_edit";
    }

    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<TableNumber> selectMyPage(VoPage voPage, TableNumber tableNumber){
        return tableNumberService.selectMyPage(voPage,tableNumber,getUser().getId());
    }

    /**
     * @param tableNumber
     * @return 添加和修改
     */
    @RequestMapping(value = "saveOrUpdate")
    @ResponseBody
    public ApiResult saveOrUpdate(@RequestBody TableNumber tableNumber){
        if(tableNumber.getId()!=null){
            if(tableNumberService.updateById(tableNumber)){
                return ApiResult.ok();
            }
        }else{
            //根据桌号和商家id查询 不能又同样的桌号
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",getUser().getId());
            List<TableNumber> tableNumbers = tableNumberService.list(queryWrapper);
            for (int i = 0; i < tableNumbers.size(); i++) {
                if(tableNumber.getName().equals(tableNumbers.get(i).getName())){
                    return ApiResult.fail("桌号重复,请重新添加");
                }
            }
            tableNumber.setCreateTime(System.currentTimeMillis());
            tableNumber.setUserId(getUser().getId());
            tableNumber.setState(1);
            if(tableNumberService.save(tableNumber)){
                return ApiResult.ok();
            }
        }
        return ApiResult.fail();
    }


    /**
     * @param id
     * @return 删除
     */
    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult deleteById(Long id){
        if(tableNumberService.removeById(id)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param userId
     * @return 根据商家查询桌号
     */
    @RequestMapping(value = "selectName")
    @ResponseBody
    public ApiResult selectName(Long userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return ApiResult.ok(tableNumberService.list(queryWrapper));
    }


}
